package com.david.mydemo.ctl_ftp.service.impl;

import com.david.mydemo.ctl_ftp.eunm.AaaaEnum;
import com.david.mydemo.util.MyGetIpAdderss;
import com.david.mydemo.ctl_ftp.dao.SysLogDao;
import com.david.mydemo.ctl_ftp.model.AaaaLog;
import com.david.mydemo.ctl_ftp.model.AaaaLogRspInfo;
import com.david.mydemo.ctl_ftp.model.DataEntity;
import com.david.mydemo.ctl_ftp.service.ISysLogService;
import com.david.mydemo.util.DateUtils;
import com.david.mydemo.util.FtpClientUtil;
import com.david.mydemo.util.XmlParserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service("SysLogService")
@Slf4j
public class SysLogService implements ISysLogService {

    @Autowired
    private SysLogDao sysLogDao;

    @Autowired
    private HttpServletRequest request;

    @Value("${ftp.hostname}")
    private String hostname;

    @Value("${ftp.port}")
    private int port;

    @Value("${ftp.username}")
    private String username;

    @Value("${ftp.password}")
    private String password;

    @Value("${ftp.ftpPath}")
    private String ftpPath;

    @Override
    public void saveSysLog(String userId, String operationType, String module, String content) {
        log.info("保存日志。。。");
    }

    @Override
    public int saveLogOn(AaaaLog aaaaLog) {
        aaaaLog.setAppLogId(UUID.randomUUID().toString().replace("-", ""));
        aaaaLog.setSessionId(UUID.randomUUID().toString().replace("-", ""));
        aaaaLog.setOperateTime(DateUtils.dateTimeNow());
        aaaaLog.setOpLevelId("1");
        aaaaLog.setClientAddress(MyGetIpAdderss.getIpAddress(request));
        aaaaLog.setClientNetworkAddress(MyGetIpAdderss.getIpAddress(request));
        aaaaLog.setOperateResult("0");
        aaaaLog.setSubAccountName(aaaaLog.getSubAccountName());
        aaaaLog.setModuleId(AaaaEnum.LOGON.getType());
        aaaaLog.setModuleName(AaaaEnum.LOGON.getName());
        aaaaLog.setOpTypeId(AaaaEnum.OPONE.getType());
        aaaaLog.setOpTypeName(AaaaEnum.OPONE.getName());
        aaaaLog.setOperateContent(aaaaLog.getSubAccountName() + "进行了" + aaaaLog.getOpTypeName());
        return sysLogDao.saveAaaaLog(aaaaLog);
    }

    @Override
    public List<AaaaLog> queryAaaaLog(DataEntity dataEntity) {
        return sysLogDao.queryAaaaLog(dataEntity.getCurrentDate());
    }

    @Override
    public String exportFtpXmlData(String lastDateStr) {
        String newLastDateStr = null;
        try {
            List<AaaaLog> logList = sysLogDao.queryAaaaLog(lastDateStr);
            // 更新最后执行时间为当前时间
            newLastDateStr = DateUtils.getTime();
            if (CollectionUtils.isEmpty(logList)) {
                log.info("查询指定时间[{}]返回日志列表为空。", lastDateStr);
                return newLastDateStr;
            }

            AaaaLogRspInfo aaaaLogRspInfo = new AaaaLogRspInfo();
            aaaaLogRspInfo.setAaaaLogList(logList);
            String parse2Xml = XmlParserUtil.parse2Xml(aaaaLogRspInfo, AaaaLogRspInfo.class);

            log.info("xml报文内容：{}", parse2Xml);
            InputStream inputStream = new ByteArrayInputStream(parse2Xml.getBytes());

            Integer code = new Random().nextInt(9999);
            String fileName = "GSNGGIS_" + DateUtils.dateTimeNow() + (code < 1000 ? code + 1000 : code) + ".xml";

            FtpClientUtil ftpClientUtil = FtpClientUtil.getInstance(hostname, port, username, password);
            ftpClientUtil.uploadFileToFtp(ftpPath, fileName, inputStream);

            return newLastDateStr;
        } catch (Exception e) {
            log.error("导出xml日志文件，上传到审计ftp服务器，错误信息：", e);
            return newLastDateStr;
        }
    }

}
